
package com.loong.loong.hik.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.loong.common.core.util.R;
import com.loong.loong.hik.entity.CdcDept;
import com.loong.loong.hik.mapper.CdcDeptMapper;
import com.loong.loong.hik.property.urls.face.HikFaceUrl;
import com.loong.loong.hik.service.CdcDeptService;
import com.loong.loong.hik.service.HikCdcVService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 疾控中心部门表
 *
 * @author cs
 * @date 2023-04-13 10:24:56
 */
@Service
public class CdcDeptServiceImpl extends ServiceImpl<CdcDeptMapper, CdcDept> implements CdcDeptService {
	@Resource
	private HikCdcVService hikCdcVService;

	@Override
	public R add(CdcDept cdcDept) {
		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				R r = hikCdcVService.updateDept(cdcDept);
				if (r==null||r.getCode()!=0){
					return R.failed("接口调用失败，原因："+r.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
				return R.failed("接口调用异常"+e.getMessage());
			}
		}

		CdcDept cdcDept1 = new CdcDept();

		cdcDept1.setDeptnumber(cdcDept.getDeptnumber());
		long count = count(Wrappers.query(cdcDept1));

		if (count>0){
			return R.failed("已存在相同的账户："+cdcDept1.getDeptnumber());
		}else{
			return R.ok(save(cdcDept));
		}
	}

	@Override
	public R del(Integer id) {
		CdcDept byId = getById(id);
		if (byId==null){
			R.failed("删除失败，根据id查询为空");
		}
		if (StringUtils.isEmpty(byId.getDeptnumber())){
			R.failed("删除失败，部门编号为空");
		}
		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				R r = hikCdcVService.deleteDept(byId);
				if (r==null||r.getCode()!=0){
					return R.failed("接口调用失败，原因："+r.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return R.ok(removeById(id));
	}

	@Override
	public R update(CdcDept cdcDept) {

		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				R r = hikCdcVService.updateDept(cdcDept);
				if (r==null||r.getCode()!=0){
					return R.failed("接口调用失败，原因："+r.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return R.ok(updateById(cdcDept));
	}

}
